package grafica.ventanas;

import grafica.controladores.ControladorEmpresasListado;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import logica.datatypes.DataEmpresa;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;


public class EmpresasListado {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="375,21"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTable jTableEmpresas = null;
	private ControladorEmpresasListado ConEmpresasListado;
	private JPanel jPanel1 = null;
	private JButton jButton = null;
	public EmpresasListado() {
		ConEmpresasListado = new ControladorEmpresasListado(this);
	}
	
	public void setVisible (boolean b){	
		this.getJFrame().setVisible(b);
	}

	/**
	 * This method initializes jFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(273, 230));
			jFrame.setTitle("Listado de Empresas");
			jFrame.setContentPane(getJContentPanel());
			jFrame.setLocationRelativeTo(null);	
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPanel() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.NORTH);
			//jContentPane.add(getJTableEmpresas(), BorderLayout.CENTER);
			jContentPane.add(getJPanel1(), BorderLayout.NORTH);
			jContentPane.add(getJButton(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
		}
		return jPanel;
	}



	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.gridheight = 1;
			gridBagConstraints2.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			//jPanel1.add(scrollPane, gridBagConstraints);
			jPanel1.add(getJTableEmpresas(), gridBagConstraints2);
		}
		return jPanel1;
	}
	
	/**
	 * This method initializes jTableEmpresas	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableEmpresas() {
		if (jTableEmpresas == null) {
			CargarEmpresas();
		}
		return jTableEmpresas;
	}
	
	
	
	private void CargarEmpresas(){
		DataEmpresa[] Empresas = ConEmpresasListado.getEmpreas();
		int cantEmp = Empresas.length;
		Object[] data = new Object[cantEmp+1];
		DefaultTableModel dtm= new DefaultTableModel();		   	       
		String[] columnas={"RUT","RAZON SOCIAL"};

		for(int column = 0; column < 2; column++){
			dtm.addColumn(columnas[column]);
			data[column] = columnas[column];
		}   
		dtm.addRow(data);
		// insertamos el contenido de las columnas

		for(int row = 0; row < cantEmp; row++) {      
			data[0] =   Empresas[row].getRut();                
			data[1] =  Empresas[row].getRazon_Social();
			dtm.addRow(data);                
		}   

		jTableEmpresas = new JTable(dtm);		 
		
		jTableEmpresas.setPreferredScrollableViewportSize(new Dimension(100, 70));
		jTableEmpresas.setBackground(new Color(0,0,0,0));  
		JScrollPane scrollPane = new JScrollPane(jTableEmpresas);
		jTableEmpresas.setPreferredSize(new java.awt.Dimension(200,150));
	
	}
	

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Volver");
			jButton.setPreferredSize(new java.awt.Dimension(90,25));
			ActionListener BotonVolver = new ActionListener(){
				public void actionPerformed (ActionEvent e){ 				
					jFrame.dispose();				
				}
			};
			jButton.addActionListener(BotonVolver);
		}
		return jButton;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
